package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void add(ShoppingCartDTO cart) {
        log.info("添加购物车：{}", cart);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(cart, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断是否存在该商品
        //如果存在，数量+1
        ShoppingCart s = shoppingCartMapper.list(shoppingCart);
        if(s!= null){
            s.setNumber(s.getNumber()+1);
            shoppingCartMapper.updateNum(s);
        }
        else{
        //判断是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if(dishId!= null){
            //菜品
            Dish dish = dishMapper.selectById(dishId);
            shoppingCart.setDishId(dishId);
            shoppingCart.setName(dish.getName());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setImage(dish.getImage());
        }
        else{
            //套餐
            Setmeal setmeal = setMealMapper.get(shoppingCart.getSetmealId());
            shoppingCart.setSetmealId(setmeal.getId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setImage(setmeal.getImage());
        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list(Long userId) {
        List<ShoppingCart> list = shoppingCartMapper.findById(userId);
        return list;
    }

    //删除购物车中一个商品
    @Override
    public void sub(ShoppingCartDTO cart) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(cart, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断购物车中该商品是否存在多于1个
        ShoppingCart s = shoppingCartMapper.list(shoppingCart);
        if(s.getNumber() > 1){
            s.setNumber(s.getNumber()-1);
            shoppingCartMapper.updateNum(s);
        }
        else {shoppingCartMapper.delete(shoppingCart);}
    }


    //清空购物车
    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);
    }
}
